local youxiang = fk.CreateSkill{
  name = "wk_heg__youxiang",
}

Fk:loadTranslationTable{
  ["wk_heg__youxiang"] = "游降",
  [":wk_heg__youxiang"] = "出牌阶段限一次，你可以与一名与你势力不同的角色拼点，若你：赢，其<a href=':DoGiveDeputy'>遣调</a>一张其已明置的武将牌于你至此回合结束；没赢，其观看你所有手牌并获得其中点数最大的一张。",
  
  ["#wk_heg__youxiang_delay"] = "游降",
  ["#wk_heg__youxiang"] = "游降：你可以与一名与你势力不同的角色拼点",
  ["#wk_heg__youxiang-ask"] = "游降：选择一张武将牌遣调给 %dest",
  ["#wk_heg__youxiang-get"] = "游降：选择一张点数最大的牌获得",

  ["$wk_heg__youxiang1"] = "合则两利，斗则两伤。",
  ["$wk_heg__youxiang2"] = "君且安坐，听我一言。",
}

local H = require "packages/ol_hegemony/util"
local HG = require "packages/new_heg_diy_my/util"

youxiang:addEffect("active", {
  anim_type = "special",
  target_num = 1,
  prompt = "#wk_heg__youxiang",
  card_num = 0,
  max_phase_use_time = 1,
  can_use = function (self, player)
    return not player:isKongcheng() and player:usedEffectTimes(self.name, Player.HistoryPhase) == 0
  end,
  card_filter = Util.FalseFunc,
  target_filter = function (self, player, to_select, selected)
    return #selected == 0 and to_select ~= player and H.compareKingdomWith(player, to_select, true) and player:canPindian(to_select)
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    local pindian = player:pindian({target}, youxiang.name)
    if player.dead or target.dead then return end
    if pindian.results[target].winner == player then
      if player:getMark("@wk_give_deputy") ~= 0 then return end
      local all_choices = {target.general, target.deputyGeneral}
      local disable_choices = {}
      if not (target.general ~= "anjiang" and not target.general:startsWith("blank_") and not string.find(target.general, "lord")) then -- 耦合君主
        table.insert(disable_choices, target.general)
      end
      if not (target.deputyGeneral ~= "anjiang" and not target.deputyGeneral:startsWith("blank_")) then
        table.insert(disable_choices, target.deputyGeneral)
      end
      if #disable_choices == 2 then return false end
      local result = room:askToCustomDialog(target, {
        skill_name = youxiang.name,
        qml_path = "packages/utility/qml/ChooseGeneralsAndChoiceBox.qml",
        extra_data = {
          all_choices,
          {"OK"},
          "#wk_heg__youxiang-ask::" .. player.id,
          {},
          1,
          1,
          disable_choices,
        }
      })
      local choice
      if result ~= "" then
        local reply = result
        choice = reply.cards[1]
      else
        choice = table.find(all_choices, function(g) return not table.contains(disable_choices, g) end)
      end
      local isDeputy = choice == target.deputyGeneral
      HG.DoGiveDeputy(room, target, player, choice, target.kingdom, isDeputy)
      room:setPlayerMark(player, "wk_give_deputy_jianyong", 1)
      room:setPlayerMark(player, "wk_heg__youxiang-record", choice)
    else
      if not player:isKongcheng() then
        local mcards, max = {}, -1
        for _, cid in ipairs(player:getCardIds("h")) do
          local c = Fk:getCardById(cid)
          if c.number > max then
            mcards = {cid}
            max = c.number
          elseif c.number == max then
            table.insert(mcards, cid)
          end
        end
        local card, _ = room:askToChooseCardsAndChoice(target,{
          cards = mcards,
          choices = {"OK"},
          skill_name = youxiang.name,
          prompt = "#wk_heg__youxiang-get",
          min_num = 1,
          max_num = 1,
          all_cards = player:getCardIds("h"),
        })
        room:obtainCard(target, card, true, fk.ReasonPrey)
      end
    end
  end,
})

youxiang:addEffect(fk.TurnEnd, {
  anim_type = "special",
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return player == target and player:getMark("wk_give_deputy_jianyong") == 1 and player:getMark("@wk_give_deputy") == player:getMark("wk_heg__youxiang-record")
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    HG.StopGiveDeputy(player.room, player, youxiang.name)
    player.room:setPlayerMark(player, "wk_give_deputy_jianyong", 0)
    player.room:setPlayerMark(player, "wk_heg__youxiang-record", 0)
  end,
})

return youxiang